<template>
    <div class="app-container">
        <el-row :gutter="20">
            <!--数据-->
            <el-col :span="24" :xs="24">
                <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
                    <el-form-item :label="$locale('geWudevice.add.Experiment Name')" prop="experimentName">
                        <el-input v-model="queryParams.experimentName" :placeholder="$locale('geWuExperiment.Please enter the experiment name')" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item :label="$locale('geWudevice.add.state')" prop="status">
                        <el-select v-model="queryParams.status" :placeholder="$locale('geWudevice.add.state')" clearable style="width: 240px">
                            <el-option v-for="dict in dict.type.experimental_status" :key="dict.value" :label="dict.label" :value="+dict.value" />
                        </el-select>
                    </el-form-item>
                    <el-form-item :label="$locale('geWuExperiment.grade')" prop="level">
                        <el-select v-model="queryParams.level" :placeholder="$locale('geWuExperiment.grade')" multiple clearable style="width: 240px">
                            <el-option v-for="dict in levelList" :key="dict.id" :label="dict.levelName" :value="dict.id" />
                        </el-select>
                    </el-form-item>
                    <el-form-item :label="$locale('geWuExperiment.difficulty')" prop="difficultyLevel">
                        <el-select v-model="queryParams.difficultyLevel" :placeholder="$locale('geWuExperiment.difficulty')" multiple clearable style="width: 240px">
                            <el-option v-for="dict in dict.type.difficulty_level" :key="dict.value" :label="dict.label" :value="dict.value" />
                        </el-select>
                    </el-form-item>
                    <el-form-item :label="$locale('geWuExperiment.Experimental time')" prop="experimentTime">
                        <el-select v-model="queryParams.experimentTime" :placeholder="$locale('geWuExperiment.Experimental time')" multiple clearable style="width: 240px">
                            <el-option v-for="dict in dict.type.experiment_time" :key="dict.value" :label="dict.label" :value="dict.value" />
                        </el-select>
                    </el-form-item>
                    <el-form-item :label="$locale('geWuExperiment.Is it publicly available')" prop="isOpen">
                        <el-select v-model="queryParams.isOpen" :placeholder="$locale('geWuExperiment.Is it publicly available')" clearable>
                            <!-- <el-option v-for="dict in dict.type.whether_not" :key="dict.value" :label="dict.label" :value="dict.value" /> -->
                            <el-option label="是" :value="true" />
                            <el-option label="否" :value="false" />
                        </el-select>
                    </el-form-item>
                    <el-form-item :label="$locale('geWuExperiment.classification')" prop="experimentType">
                        <el-select v-model="queryParams.experimentType" :placeholder="$locale('geWuExperiment.classification')" multiple clearable style="width: 240px">
                            <el-option v-for="dict in dict.type.experiment_type" :key="dict.value" :label="dict.label" :value="dict.value" />
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $locale('geWudevice.add.search for') }}</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $locale('geWudevice.reset') }}</el-button>
                    </el-form-item>
                </el-form>

                <!-- <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
                    </el-col>
                </el-row> -->

                <el-table v-loading="loading" :data="geWuExperimentList">
                    <el-table-column :label="$locale('geWudevice.add.Serial Number')" type="index" />
                    <el-table-column :label="$locale('geWudevice.add.Experiment Name')" align="center" key="experimentName" prop="experimentName" />
                    <el-table-column :label="$locale('geWuExperiment.author')" align="center" key="createBy" prop="createBy" />
                    <el-table-column :label="$locale('geWudevice.Creation time')" align="center" prop="createTime" width="160">
                        <template slot-scope="scope">
                            <span>{{ parseTime(scope.row.createTime) }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column :label="$locale('geWuExperiment.classification')" align="center" key="experimentType" prop="experimentType" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <span v-for="item in dict.type.experiment_type" :key="item.value">{{ item.value == scope.row.experimentType ? item.label : '' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column :label="$locale('geWuExperiment.Experimental time')" align="center" key="experimentTime" prop="experimentTime" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <span v-for="item in dict.type.experiment_time" :key="item.value">{{ item.value == scope.row.experimentTime ? item.label : '' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column :label="$locale('geWuExperiment.difficulty')" align="center" key="difficultyLevel" prop="difficultyLevel" width="120">
                        <template slot-scope="scope">
                            <span v-for="item in dict.type.difficulty_level" :key="item.value">{{ item.value == scope.row.difficultyLevel ? item.label : '' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column :label="$locale('geWudevice.add.state')" align="center" key="status">
                        <template slot-scope="scope">
                            <span v-for="item in dict.type.experimental_status" :key="item.value">{{ item.value == scope.row.status ? item.label : '' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column :label="$locale('geWuExperiment.Is it publicly available')" align="center" key="isOpen">
                        <template slot-scope="scope">
                            <span>{{ scope.row.isOpen ? $locale('geWuExperiment.yes') : $locale('geWuExperiment.no') }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column :label="$locale('geWuExperiment.source')" align="center" key="experimentSource" prop="experimentSource" />
                    <el-table-column :label="$locale('formMarkering.list.operate')" align="center" width="160" class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" @click="handleUpdate(scope.row, true)">{{ $locale('geWudevice.view') }}</el-button>
                            <el-button size="mini" type="text" @click="handleClone(scope.row)">{{ $locale('geWuExperiment.clone') }}</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
            </el-col>
        </el-row>

        <!-- 添加 -->
        <addExperiment ref="addExperiment" @getList="getList"></addExperiment>
    </div>
</template>

<script>
import { deleteById, experimentPageV2, cloneExperiment } from '@/api/geWuExperiment';
import { getLevelList } from '@/api/myClass/levelList';
import addExperiment from '@/views/myExperiment/experimentalList/add/index.vue';
import { getNames } from '@/utils/index';

// 格悟实验
export default {
    name: 'geWuExperiment',
    dicts: ['experimental_status', 'difficulty_level', 'experiment_time', 'experiment_type', 'whether_not'],
    components: { addExperiment },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            geWuExperimentList: [],
            levelList: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                experimentName: undefined,
                difficultyLevel: [],
                level: [],
                status: undefined,
                isOpen: undefined,
                experimentTime: [],
                experimentType: []
            }
        };
    },
    created() {
        this.getList();
        getLevelList().then(({ data }) => {
            this.levelList = data;
        });
    },
    methods: {
        /** 查询用户列表 */
        getList() {
            this.loading = true;
            experimentPageV2(this.queryParams).then(({ data }) => {
                this.geWuExperimentList = data.records;
                this.total = data.total;
                this.loading = false;
            });
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.$refs.addExperiment.handleAdd();
        },
        /** 修改按钮操作 */
        handleUpdate(row, disabled) {
            this.$refs.addExperiment.handleUpdate(row, disabled);
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const userIds = row.id || this.ids;
            this.$modal
                .confirm(this.$locale('geWudevice.tip1') + getNames(this.geWuExperimentList, userIds, 'experimentName') + this.$locale('geWudevice.tip2') )
                .then(function () {
                    return deleteById({ id: userIds });
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess(this.$locale('geWudevice.tip3'));
                })
                .catch(() => {});
        },
        /** 克隆按钮操作 */
        handleClone(row) {
            cloneExperiment(row.id).then((response) => {
                this.$modal.msgSuccess(this.$locale('geWuExperiment.Cloning successful'));
            });
        }
    }
};
</script>
